package cn.tom.service.impl;

import cn.tom.dao.BookDao;
import cn.tom.dao.OrderDao;
import cn.tom.dao.OrderitemDao;
import cn.tom.entity.*;
import cn.tom.service.BookService;
import cn.tom.service.CartService;
import cn.tom.service.OrderService;
import cn.tom.service.OrditemService;
import cn.tom.tools.TokenAndUser;
import com.sun.org.apache.xpath.internal.operations.Or;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {

    @Autowired(required = false)
    private OrderDao orderDao;
    @Autowired(required = false)
    private OrditemService orditemService;
    @Autowired
    private BookService bookService;
    @Autowired
    private CartService cartService;

    @Override
    public int updateShip(Integer ordid, Integer ship, String shipno, String shipper, Date date) {
        HashMap<String,Object> map = new HashMap<>();
        map.put("ordid",ordid);
        map.put("shipStatus",ship);
        map.put("tship",date);//发货时间
        map.put("shipno",shipno);
        map.put("shipper",shipper);
        return orderDao.update(map);
    }

    @Override
    public int updateSign(Integer ordid, Integer ship, String shipno, String shipper, Date date) {
        HashMap<String,Object> map = new HashMap<>();
        map.put("ordid",ordid);
        map.put("shipStatus",ship);
        map.put("tSign",date);//签收时间
        return orderDao.update(map);
    }

    @Override
    public Order findOrdByUid(Integer uid) {
        return orderDao.findByid(uid,null);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int doPay(Integer ordid) throws MyException{
        int n = 0;
        Order byOrdid = this.findByOrdid(ordid);
        for(Orditem o : byOrdid.getOrditems()){
            Book one = bookService.findOne(o.getBook().getId());
            if( o.getNum() > one.getStock())throw new MyException("付款失败，库存不足无法出库");
            one.setStock(one.getStock() - o.getNum());
             bookService.update(one);
        }
        HashMap<String,Object> map = new HashMap<>();
        map.put("ordid",byOrdid.getOrdid());
        map.put("tpay",new Date());
        map.put("payStatus",1);
        map.put("shipStatus",0);
         n = orderDao.update(map);
        return n;
    }

    @Override
    public Order findByOrdid(Integer ordid) {
        return  orderDao.findByid(null , ordid);
    }

    @Override
    public List<Order> findOrdersByPages(HashMap<String,Object> map , Integer pagenum,Integer lines){
        pagenum = (pagenum -1) * lines;
        map.put("pagenum" , pagenum);
        map.put("lines",lines);
        return orderDao.findOrderByPages(map);
    }

    @Override
    public int total(HashMap<String, Object> map) {
        return orderDao.total(map);
    }


    /**
     *  1.生产订单头
     *  2.如果订单编号 等于 0 或者 下单的商品数量 等于 0 抛异常
     *  3.删除购物车的商品 并添加 订单明细
     * @param cart2Ord
     * @return
     * @throws MyException
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Order add2Order(Cart2Ord cart2Ord) throws MyException{
        XUser xUser = (XUser) TokenAndUser.findOne().getData();
        Order order = new Order();
        order.getMyUser().setId(xUser.getId());
        order.setTcreate(new Date());//创建订单时间
        order.setPay_status(0);//未付款
        //收货人 姓名 、 电话 、 地址
        order.setShip2addr(cart2Ord.getShip2addr());
        order.setShip2name(cart2Ord.getShip2name());
        order.setShip2phone(cart2Ord.getShip2phone());
        //拿总价
        double amount = 0.0;
        for (int i = 0; i < cart2Ord.getCartList().size(); i++) {
            amount += cart2Ord.getCartList().get(i).getPrice();
        }
        order.setAmount(amount);
        orderDao.add(order);
        if(order.getOrdid() == 0)throw new MyException("下单失败");
        if(cart2Ord.getCartList().size() == 0) throw new MyException("下单失败");
        for (Cart cart : cart2Ord.getCartList()) {
            Orditem orditem = new Orditem();
            orditem.getOrder().setOrdid(order.getOrdid()); //添加后的id
            orditem.getBook().setId(cart.getBook().getId());
            orditem.setNum(cart.getNum());
            orditem.setPrice(cart.getPrice());
            int del = cartService.del(cart.getId());
            if(del == 1) orditemService.add(orditem);
        }
        return order;
    }
}
